package com.gee.spring.boot.oauth2.auth.config.oauth2.token;

import com.gee.spring.boot.oauth2.auth.entity.AuthUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * desc:
 *
 * @author gee
 * @date 2020/04/06 18:26
 */
public class GeeUserAuthenticationConverter extends DefaultUserAuthenticationConverter {
    @Override
    public Map<String, ?> convertUserAuthentication(Authentication authentication) {
        AuthUser authUser = (AuthUser)authentication.getPrincipal();
        Map<String, Object> response = new LinkedHashMap<>();
        response.put("user_name", authUser.getUsername());
        response.put("user_id", authUser.getId());
        if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
            response.put("authorities", AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
        }
        return response;
    }
}
